package cn.tj.wenda.cloud.util;

import cn.tj.wenda.cloud.entity.LoginUserInfo;
import cn.tj.wenda.cloud.security.JsonWebToken;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class LoginUser {

    /**
     * 获取当前用户的Token串
     * @return
     */
    public  static String getLoginToken(){
        String token = "";
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = (HttpServletRequest) servletRequestAttributes.getRequest();

        //从header中取token
        if(request.getHeader(ConstUtil.TOKEN) != null && !"".equals(request.getHeader(ConstUtil.TOKEN))){
            Enumeration<?> e=request.getHeaders(ConstUtil.TOKEN);
            while(e.hasMoreElements()){
                token = (String)e.nextElement();//调用nextElement方法获得元素
            }
        }
        return token;
    }

    /**
     * 获取当前用户的实体信息
     * @return
     */
    public static LoginUserInfo getLoginUserInfo(){
        String token = LoginUser.getLoginToken();
        LoginUserInfo loginUserInfo = JsonWebToken.getPayload(token);
        return loginUserInfo;
    }

    /**
     * 获取当前登录人所在的所有租户ID列表
     * @return 租户IDList
     */
    public static List<String> getTenantList(){
        String token = LoginUser.getLoginToken();
        return LoginUser.getTenantList(token);
    }

    /**
     * 获取给定Token所在的所有租户ID列表
     * @param token
     * @return
     */
    public static List<String> getTenantList(String token){
        LoginUserInfo loginUserInfo = JsonWebToken.getPayload(token);

        //测试数据
        List<String> lstTenants = new ArrayList<>();
        lstTenants.add("0");
        lstTenants.add("0112");

        return lstTenants;
    }

    /**
     * 获取当前登录人所在的所有租户的SQL查询形式，不带and
     * @return
     */
    public static String getTenantWhere(){
        String token = LoginUser.getLoginToken();
        return LoginUser.getTenantWhere(token);
    }

    /**
     * 获取当给定Token所在的所有租户的SQL查询形式，不带and
     * @param token
     * @return
     */
    public static String getTenantWhere(String token){
        List<String> lstTenants = LoginUser.getTenantList(token);
        String sqlWhere = StringUtils.join(lstTenants,"','");
        sqlWhere = " tenantid IN ('" + sqlWhere + "') ";
        return sqlWhere;
    }

    /**
     * 获取当前登录人所能看到的数据用户ID列表，用于数据层面的权限控制
     * @return
     */
    public static List<String> getAddUserIdList(){
        String token = LoginUser.getLoginToken();
        return LoginUser.getAddUserIdList(token);
    }

    /**
     * 获取给定Token所能看到的数据用户ID列表，用于数据层面的权限控制
     * @param token
     * @return
     */
    public static List<String> getAddUserIdList(String token){
        LoginUserInfo loginUserInfo = JsonWebToken.getPayload(token);

        //测试数据
        List<String> lstAddUserIds = new ArrayList<>();
        lstAddUserIds.add("1002018030216023752700001");//wenda
        lstAddUserIds.add("1002018030614465442900028");//管理员

        return lstAddUserIds;
    }

    public static String getAddUserIdWhere() {
        //TODO 根据数据权限方式构建sql
        return "";
    }

    public static String getAddUserIdWhere(String token) {
        //TODO 根据数据权限方式构建sql
        return "";
    }

    public static String getAuthorityWhere(){
        String token = LoginUser.getLoginToken();

        return LoginUser.getAuthorityWhere(token);
    }

    public static String getAuthorityWhere(String token){
        String sql = LoginUser.getTenantWhere(token);
        String addUserIdSQL = LoginUser.getAddUserIdWhere(token);
        if(StringUtils.isNotEmpty(addUserIdSQL)){
            sql += " AND " + addUserIdSQL;
        }

        return sql;
    }
}
